define(["jquery", "easy-admin"], function ($, ea) {

    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'shop/index/district_id/',
        add_url: 'shop/add',
        edit_url: 'shop/edit',
        edit1_url: 'shop/edit1',
        delete_url: 'shop/delete',
        export_url: 'shop/export',
        modify_url: 'shop/modify',
        images_url: 'shop.banner/index',
        settlement_url: 'shop/settlement',
    };

    var Controller = {

        index: function () {
            ea.table.render({
                init: init,
                toolbar: ['refresh', 'add', 'delete'],
                cols: [[
                    {type: 'checkbox'},
                    {field: 'id', title: '店铺ID', search: false},
                    {field: 'district_name', title: '商圈名称'},
                    {field: 'name', title: '店铺名称', fieldAlias: 's.name'},
                    {field: 'logo', title: '店铺logo', imageHeight: 80, templet: ea.table.image, search: false},
                    {
                        field: 'status', title: '店铺状态', selectList: ['待审核', '已通过', '已关闭'], templet: function (k, v) {
                            switch (k.status) {
                                case 1:
                                    return '<span class="layui-badge layui-bg-green">已通过</span>';
                                    break;
                                case 2:
                                    return '<span class="layui-badge">已关闭</span>';
                                    break;
                                default:
                                    return '<span class="layui-badge layui-bg-gray">待审核</span>';
                                    break;
                            }
                        }
                    },
                    {field: 'nickname', title: '老板', search: false},
                    {field: 'phone', title: '联系电话', width: 200},
                    {
                        field: 'platform_goods_switch',
                        width: 120,
                        title: '工具库开关',
                        templet: ea.table.switch,
                        selectList: {0: '已关闭', 1: '已开启'}
                    },
                    {
                        field: 'share_goods_switch',
                        width: 120,
                        title: '共享库开关',
                        templet: ea.table.switch,
                        selectList: {0: '已关闭', 1: '已开启'}
                    },
                    {field: 'points_one', title: '积分1', search: false},
                    {field: 'point_limit', title: '积分限制', search: false},
                    {field: 'shop_code', title: '商家编码'},
                    // {field: 'create_time', title: '创建时间', width: 200, search: 'range'},
                    {
                        width: 300, title: '操作', templet: function (k, v) {
                            if (k.district_id != 0) {
                                v.operat.splice(1, 1);
                            }
                            return ea.table.tool(k, v);
                        },
                        operat: [
                            'edit',
                            [{
                                text: '结算',
                                url: init.settlement_url,
                                method: 'open',
                                fieldExtra: {'shop_id': 'id'},
                                auth: '',
                                class: 'layui-btn layui-btn-xs layui-btn-danger',
                                extend: 'data-full="true"',
                            }],
                            [{
                                text: '图集',
                                url: init.images_url,
                                method: 'open',
                                fieldExtra: {'shop_id': 'id'},
                                auth: 'images',
                                class: 'layui-btn layui-btn-xs layui-btn-normal',
                                extend: 'data-full="true"',
                            }],
                            [{
                                text: '设置商圈',
                                url: init.edit1_url,
                                method: 'open',
                                fieldExtra: {'shop_id': 'id'},
                                auth: 'images',
                                class: 'layui-btn layui-btn-xs layui-btn-normal',
                                extend: 'data-full="true"',
                            }],
                            'delete']
                    },
                ]],
                done: function () {
                    $(".layui-table-body>table>tbody>tr").each(function (index, val) {
                        $($(".layui-table-fixed .layui-table-body table tr")[index]).height($(val).height());
                    });
                },
            });

            ea.listen();
        },
        add: function () {
            ea.listen();
        },
        edit: function () {
            ea.listen();
        },
        edit1: function () {
            ea.listen();
        },
        settlement: function () {
            ea.listen();
            var va=0;
            $('body').on('click','#settlement',function () {

                layer.prompt({
                    formType: 0,
                    title: '请输入想要结算金额',
                }, function (value, index, elem) {
                    if(isNaN(value) || value==0){
                        return ea.msg.error('数据格式不正确');
                    }
                    layer.open({
                        title:"你要给用户变动的金额:",
                        content:toChinesNum(value),
                        btn:['增加','减少','取消'],
                        yes:function(){
                            // layer.alert('开始请求接口,参数:'+value+'type:+');
                            ea.request.post({
                                url: 'settlement2',
                                data: {
                                    id: id,
                                    type:1,
                                    num:value,
                                },
                            }, function (res) {
                                ea.msg.success(res.msg, function () {
                                    location.reload();
                                });
                            })
                            layer.closeAll()
                        },
                        btn2:function(){
                            ea.request.post({
                                url: 'settlement2',
                                data: {
                                    id: id,
                                    type:2,
                                    num:value,
                                },
                            }, function (res) {
                                ea.msg.success(res.msg, function () {
                                    location.reload();
                                });
                            })
                            layer.closeAll()
                        },
                        btn3:function(){
                            location.reload();
                            layer.closeAll()
                        },

                    })
                });
            });

            /**
             * 数字转成汉字
             * @params num === 要转换的数字
             * @return 汉字
             * */
            function toChinesNum(num){
                let changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
                let unit = ['', '十', '百', '千', '万']
                num = parseInt(num)
                let getWan = (temp) => {
                    let strArr = temp.toString().split('').reverse()
                    let newNum = ''
                    let newArr = []
                    strArr.forEach((item, index) => {
                        newArr.unshift(item === '0' ? changeNum[item] : changeNum[item] + unit[index])
                    })
                    let numArr = []
                    newArr.forEach((m, n) => {
                        if (m !== '零') numArr.push(n)
                    })
                    if (newArr.length > 1) {
                        newArr.forEach((m, n) => {
                            if (newArr[newArr.length - 1] === '零') {
                                if (n <= numArr[numArr.length - 1]) {
                                    newNum += m
                                }
                            } else {
                                newNum += m
                            }
                        })
                    } else {
                        newNum = newArr[0]
                    }

                    return newNum
                }
                let overWan = Math.floor(num / 10000)
                let noWan = num % 10000
                if (noWan.toString().length < 4) {
                    noWan = '0' + noWan
                }
                return overWan ? getWan(overWan) + '万 ' + getWan(noWan) : getWan(num)
            }
        },
        export: function () {
            ea.listen();
        },
    };
    return Controller;
});